namespace :generate do
  task :ensure_user_selected_prize=>:environment do
    prize = Prize.first
    User.all.each do |user|
      unless user.prize
        user.prize = prize
        user.save
      end
    end
  end


  task :result=>:environment do
    match = Match.next_match
    if match
      puts "current match: #{match.info}"
      print "score for #{match.home_team.name}:"
      home_score = STDIN.readline
      print "score for #{match.away_team.name}:"
      away_score = STDIN.readline

      match.play_from_point(home_score.to_f,away_score.to_f)
      puts "Next match is #{Match.next_match.info}"
    else
      puts "Next match not found."
    end
  end

  task :prize=>:environment do
    [
      {
        :name=> "Apple 3G iPhone",
        :streak=> 13,
        :pic=> "prizes/3g-iphone.png",
        :created_at=> "2008-12-13 06:46:15",
        :updated_at=> "2009-01-02 13:44:49",
        :description=> ""
      },
      {
        :name=> "Sony Ericsson W890",
        :streak=> 13,
        :pic=> "prizes/sony_ericsson_w890i_f.png",
        :created_at=> "2008-12-15 02:38:06",
        :updated_at=> "2008-12-15 02:43:57",
        :description=> ""
      },
      {
        :name=> "Nokia E71",
        :streak=> 13,
        :pic=> "prizes/nokia-e71.png",
        :created_at=> "2008-12-15 02:38:55",
        :updated_at=> "2008-12-15 02:44:03",
        :description=> ""
      },
      {
        :name=> "RIM Blackberry Bold",
        :streak=> 13,
        :pic => "prizes/rim-blackberry-bold.png",
        :created_at => "2008-12-15 02:39:40",
        :updated_at=> "2008-12-15 02:39:40",
        :description=> ""
      }
    ].each do |attributes|
      prize = Prize.find_or_initialize_by_name(attributes[:name])
      prize.streak = attributes[:streak]
      prize.save!
      prize.update_attributes attributes
    end

  end

  task :team=>:sport do
    [
      {
        :name=> "aotianlong",
        :pic=> "nba/nba_phoenix_suns.gif",
        :description=> nil,
        :created_at=> "2008-12-13 04:38:49",
        :updated_at=> "2008-12-27 01:17:55",
        :sport_id=> nil
      },
      {
        :name=> "ayowaya",
        :pic=> "nba/nba_philadelphia_76ers.gif",
        :description=> nil,
        :created_at=> "2008-12-13 05:59:46",
        :updated_at=> "2008-12-27 00:57:42",
        :sport_id=> nil
      }
    ].each do |attributes|
      team = Team.find_or_initialize_by_name(attributes[:name])
      team.sport = Sport.first
      team.save!
      team.update_attributes attributes
    end

  end

  task :sport=>:environment do
    [
      {
        :name=>"Football",
        :created_at=> "2008-12-17 13:01:02",
        :updated_at=> "2008-12-28 04:32:06",
        :position=> 1
      },
      {
        :name=> "Basketball",
        :created_at=> "2008-12-17 13:01:41",
        :updated_at=> "2008-12-28 04:32:06",
        :position=> 2
      }
    ].each do |attributes|
      sport = Sport.find_or_initialize_by_name(attributes[:name])
      sport.save!
      sport.update_attributes attributes
    end
  end

  task :league=>:environment do
  end

  desc 'generate a match'
  task :match=>:environment do
    teams = Team.all
    sport = Sport.first
    match = Match.new :home_team=>teams.first,
      :away_team=>teams.last,
      :begin_at=>1.day.from_now,
      :point_spread=>10,
      :bet_type=>"spread",
      :total_point=>100,
      :sport=>sport
    match.save!

    match = Match.new :home_team=>teams.first,
      :away_team=>teams.last,
      :begin_at=>1.day.from_now,
      :point_spread=>10,
      :bet_type=>"total",
      :total_point=>200,
      :sport=>sport
    match.save!

  end

  desc 'generate bets for each user'
  task :bets=>:environment do
    match = Match.last
    users = User.all
    users.each do |user|
      bet = user.bets.build :match=>match,:team=>match.home_team
      bet.save!
    end
  end

  desc 'process bets'
  task :process=>:environment do
    match = Match.last
    match.play_from_point 199,150
  end

  desc "generate a winstreak"
  task :goal=>:environment do
    30.times.each do
      Rake::Task['generate:match'].execute
    end
    matches = Match.all :limit=>13,:order=>"id desc",:conditions=>{:bet_type=>"spread"}
    user = User.first
    matches.each do |match|
      bet = user.bets.build :match=>match,:team=>match.home_team
      bet.save!

      match.play_from_point(199,100)
    end

  end

  desc 'process all bet progress'
  task :all=>[:ensure_user_selected_prize,:match,:bets,:process]

  desc "generate crontab config"
  task :crontab=>:environment do
    interval = ENV["interval"] || 5
    user     = ENV["user"] || "root"
    env      = Rails.env
    puts "create a file , copy code to /etc/cron.d/"
    puts ""
    puts "*/#{interval}  * * * *  cd #{RAILS_ROOT} && rake fetch:matches RAILS_ENV=#{env} && rake fetch:results RAILS_ENV=#{env}"
  end

  task :base=>[:prize,:team]

end
